import { Module } from '@nestjs/common';
import AuthController from './controller/auth.controller';
import AuthService from './service/auth.service';
import EmailService from './service/email.service';
import { RedisModule } from '@nestjs-modules/ioredis';
import { GlobalExceptionFilter } from 'libs/common/filters/GlobalException.filter';
import { NacosModule } from 'libs/nacos/nacos.module';
import nacos from 'config/nacos';
import { NacosNamingClientService } from 'libs/nacos/nacosNamingClient.service';
import { NacosConfigClientService } from 'libs/nacos/nacosConfigClient.service';
import serverConfig from '../config';
import UserApi from './api/user.api';
import RoleController from './controller/role.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import Role from './entity/role.entity';
import RoleService from './service/role.service';
import Resource from './entity/resource.entity';
import UserRoleLink from './entity/user_role.link';
import RoleResourceLink from './entity/role_resource.link';
import LUserRoleRepository from './repository/l_user_role.repository';
import lRoleResourceRepository from './repository/l_role_resource.repository';
import RoleRepository from './repository/role.repository';
import ResourceRepository from './repository/resource.repository';
import ResourceService from './service/resource.service';
import ResourceController from './controller/resource.controller';

const nacosModule = NacosModule.forRoot({
  clientOptions: {
    //配置中心
    serverAddr: nacos.url,
    namespace: nacos.namespace,
  },
  nacosNamingClientConfig: {
    //服务注册发现
    logger: console,
    serverList: nacos.url,
    namespace: nacos.namespace,
  },
});

@Module({
  imports: [
    nacosModule,
    RedisModule.forRootAsync({
      useFactory: async (
        nacosConfigClientService: NacosConfigClientService,
      ) => {
        const redisConfig = await nacosConfigClientService.getConfig(
          'redis',
          'DEFAULT_GROUP',
        );
        return {
          url: redisConfig.url,
          options: {
            db: redisConfig.db,
            password: redisConfig.password,
          },
          type: 'single',
        };
      },
      imports: [nacosModule],
      inject: [NacosConfigClientService],
    }),
    TypeOrmModule.forRootAsync({
      useFactory: async (
        nacosConfigClientService: NacosConfigClientService,
      ) => {
        const mySqlConfig = await nacosConfigClientService.getConfig(
          'mysql',
          'DEFAULT_GROUP',
        );
        return {
          type: 'mysql',
          host: mySqlConfig.host,
          port: mySqlConfig.port,
          username: mySqlConfig.username,
          password: mySqlConfig.password,
          database: mySqlConfig.database,
          entities: [Resource, Role, UserRoleLink, RoleResourceLink],
          synchronize: true,
          connectorPackage: 'mysql2',
        };
      },
      imports: [nacosModule],
      inject: [NacosConfigClientService],
    }),
    TypeOrmModule.forFeature([
      LUserRoleRepository,
      lRoleResourceRepository,
      RoleRepository,
      ResourceRepository,
    ]),
  ],
  controllers: [AuthController, RoleController, ResourceController],
  providers: [
    UserApi,
    RoleService,
    ResourceService,
    AuthService,
    EmailService,
    GlobalExceptionFilter,
  ],
})
export class MsAuthModule {
  constructor(readonly nacosNamingClientService: NacosNamingClientService) {
    //项目启动时注入服务
    nacosNamingClientService.registerInstance({
      serviceName: serverConfig.serviceName,
      instance: {
        ip: serverConfig.ip,
        port: serverConfig.port,
        instanceId: serverConfig.serviceId,
        healthy: true,
        enabled: true,
      },
    });
  }
}
